<?php

namespace App\Models;

use App\Support\Data;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'order';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'order_sn',
        'order_name',
        'order_status',
        'order_customer',
        'order_amount',
        // 'username',
        // 'phone',
        'user_id',
        'pay_status',
        'pay_amount',
        'pay_type',
        'pay_ticket',
        'pay_datetime',
        'service',
        'language',
        'manuscript',
        'reference_file',
        'additional',
        'purpose',
        'subject_area',
        'discipline',
        'is_carefree',
        'is_eval'
    ];


    public static function boot()
    {
        parent::boot();
        static::deleting(function ($order) {
            $order->orderService()->delete();
            $order->serviceEmail()->delete();
            $order->serviceSystem()->delete();
            $order->report()->delete();
            $order->syntax()->delete();
            $order->details()->delete();
            $order->profiles()->delete();
            $order->polish()->delete();
        });
    }

    // 学科领域 获取器
    public function getSubjectAreaTextAttribute($value)
    {
        return Category::find($value)->title ?? '';
    }

    // 细分学科 获取器
    public function getDisciplineTextAttribute($value)
    {
        return Category::find($value)->title ?? '';
    }

    // 学科领域 获取器
    public function getPurposeTextAttribute($value)
    {
        $data = Data::getOrderPurpose();
        return isset($data[$value]) ? $data[$value] : '未知';
    }

    public function getOrderStatusTextAttribute($value)
    {
        $orderStatus = [0 => '待支付', 1 => '已支付', 2 => '已完成', -1 => '已取消'];
        return isset($orderStatus[$value]) ? $orderStatus[$value] : '未知';
    }

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    // 订单服务
    public function orderService()
    {
        return $this->hasOne(OrderService::class, 'order_id', 'id');
    }

    // 订单投稿邮箱
    public function serviceEmail()
    {
        return $this->hasMany(OrderServiceEmail::class, 'order_id', 'id');
    }

    // 订单投稿系统注册信息
    public function serviceSystem()
    {
        return $this->hasMany(OrderServiceSystemSign::class, 'order_id', 'id');
    }

    // 订单查重报告
    public function report()
    {
        return $this->hasOne(OrderServiceReport::class, 'order_id', 'id');
    }

    // 订单语法报告
    public function syntax()
    {
        return $this->hasOne(OrderServiceSyntax::class, 'order_id', 'id');
    }

    // 订单投稿详情
    public function details()
    {
        return $this->hasMany(OrderServiceSubmitDetail::class, 'order_id', 'id');
    }

    // 订单补充资料
    public function profiles()
    {
        return $this->hasMany(OrderServiceProfile::class, 'order_id', 'id');
    }

    // 订单润色、降重
    public function polish()
    {
        return $this->hasOne(OrderServicePolishReport::class, 'order_id', 'id');
    }

    public function orderComment()
    {
        return $this->hasOne(OrderComment::class, 'order_id', 'id');
    }
}
